home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / utilities / pu017.dms / pu017.adf / Gadgets / Example3.c < prev    next >
C/C++ Source or Header  |  1990-01-30  |  9KB  |  240 lines

  1. /* Example3                                                             */
  2. /* This program will open a normal window which is connected to the     */
  3. /* Workbench Screen. The window will use all System Gadgets, and will   */
  4. /* close first when the user has selected the System gadget Close       */
  5. /* window. Inside the window we have put a Boolean gadget with the text */
  6. /* "PRESS ME". The on/off state of the gadget is toggled each time the  */
  7. /* user hitts the gadget.                                               */
  8.  
  9.  
  10.  
  11. #include <intuition/intuition.h>
  12.  
  13.  
  14.  
  15. struct IntuitionBase *IntuitionBase;
  16.  
  17.  
  18.  
  19. /* The coordinates for the box: */
  20. SHORT my_points[]=
  21. {
  22.    0,  0, /* Start at position (0,0) */
  23.   70,  0, /* Draw a line to the right to position (70,0) */
  24.   70, 10, /* Draw a line down to position (70,10) */
  25.    0, 10, /* Draw a line to the right to position (0,10) */
  26.    0,  0  /* Finish of by drawing a line up to position (0,0) */ 
  27. };
  28.  
  29. /* The Border structure: */
  30. struct Border my_border=
  31. {
  32.   0, 0,        /* LeftEdge, TopEdge. */
  33.   1,           /* FrontPen, colour register 1. */
  34.   0,           /* BackPen, for the moment unused. */
  35.   JAM1,        /* DrawMode, draw the lines with colour 1. */
  36.   5,           /* Count, 5 pair of coordinates in the array. */
  37.   my_points,   /* XY, pointer to the array with the coordinates. */
  38.   NULL,        /* NextBorder, no other Border structures are connected. */
  39. };
  40.  
  41.  
  42.  
  43. /* The text string: */
  44. UBYTE my_string[]="PRESS ME";
  45.  
  46. /* The IntuiText structure: */
  47. struct IntuiText my_text=
  48. {
  49.   1,         /* FrontPen, colour register 1. */
  50.   0,         /* BackPen, colour register 0. */
  51.   JAM1,      /* DrawMode, draw the characters with colour 1, do not */
  52.              /* change the background. */ 
  53.   4, 2,      /* LeftEdge, TopEdge. */
  54.   NULL,      /* ITextFont, use default font. */
  55.   my_string, /* IText, the text that will be printed. */
  56.              /* (Remember my_text = &my_text[0].) */
  57.   NULL,      /* NextText, no other IntuiText structures are connected. */
  58. };
  59.  
  60.  
  61.  
  62. struct Gadget my_gadget=
  63. {
  64.   NULL,          /* NextGadget, no more gadgets in the list. */
  65.   40,            /* LeftEdge, 40 pixels out. */
  66.   20,            /* TopEdge, 20 lines down. */
  67.   71,            /* Width, 71 pixels wide. */
  68.   11,            /* Height, 11 pixels lines heigh. */
  69.   GADGHCOMP,     /* Flags, when this gadget is highlighted, the gadget */
  70.                  /* will be rendered in the complement colours. */
  71.                  /* (Colour 0 (00) will be changed to colour 3 (11) */
  72.                  /* (Colour 1 (01)           - " -           2 (10) */
  73.                  /* (Colour 2 (10)           - " -           1 (01) */
  74.                  /* (Colour 3 (11)           - " -           0 (00) */  
  75.   GADGIMMEDIATE| /* Activation, our program will recieve a message when */
  76.                  /* the user has selected this gadget. */
  77.   TOGGLESELECT,  /* The on/off state of the gadget is toggled each time. */
  78.   BOOLGADGET,    /* GadgetType, a Boolean gadget. */
  79.   (APTR) &my_border, /* GadgetRender, a pointer to our Border structure. */
  80.                  /* (Since Intuition does not know if this will be a */
  81.                  /* pointer to a Border structure or an Image structure, */
  82.                  /* Intuition expects an APTR (normal memory pointer). */
  83.                  /* We will therefore have to calm down the compiler by */
  84.                  /* doing some "casting".) */
  85.   NULL,          /* SelectRender, NULL since we do not supply the gadget */
  86.                  /* with an alternative image. (We complement the */
  87.                  /* colours instead) */
  88.   &my_text,      /* GadgetText, a pointer to our IntuiText structure. */
  89.                  /* (See chapter 3 GRAPHICS for more information) */
  90.   NULL,          /* MutualExclude, no mutual exclude. */
  91.   NULL,          /* SpecialInfo, NULL since this is a Boolean gadget. */
  92.                  /* (It is not a Proportional/String or Integer gdget) */
  93.   0,             /* GadgetID, no id. */
  94.   NULL           /* UserData, no user data connected to the gadget. */
  95. };
  96.  
  97.  
  98.  
  99. /* Declare a pointer to a Window structure: */ 
  100. struct Window *my_window;
  101.  
  102. /* Declare and initialize your NewWindow structure: */
  103. struct NewWindow my_new_window=
  104. {
  105.   50,            /* LeftEdge    x position of the window. */
  106.   25,            /* TopEdge     y positio of the window. */
  107.   200,           /* Width       200 pixels wide. */
  108.   100,           /* Height      100 lines high. */
  109.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  110.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  111.   CLOSEWINDOW|   /* IDCMPFlags  The window will give us a message if the */
  112.                  /*             user has selected the Close window gad, */
  113.   GADGETDOWN,    /*             or a gadget has been pressed on. */
  114.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  115.   WINDOWCLOSE|   /*             Close Gadget. */
  116.   WINDOWDRAG|    /*             Drag gadget. */
  117.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  118.   WINDOWSIZING|  /*             Sizing Gadget. */
  119.   ACTIVATE,      /*             The window should be Active when opened. */
  120.   &my_gadget,    /* FirstGadget A pointer to my_gadget structure. */
  121.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  122.   "TOGGLE ME",   /* Title       Title of the window. */
  123.   NULL,          /* Screen      Connected to the Workbench Screen. */
  124.   NULL,          /* BitMap      No Custom BitMap. */
  125.   140,           /* MinWidth    We will not allow the window to become */
  126.   50,            /* MinHeight   smaller than 140 x 50, and not bigger */
  127.   300,           /* MaxWidth    than 300 x 200. */
  128.   200,           /* MaxHeight */
  129.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  130. };
  131.  
  132.  
  133.  
  134. /*************************************************************************/
  135. /* Extra information:                                                    */
  136. /* You first need to decide what messages the gadgets should report.     */
  137. /* In this case we told the Boolean gadget to send a message if the user */
  138. /* pressed on it. (We sat the flag GADGIMMEDIATE) (The Close window      */
  139. /* gadget will always send a message if someone has selected it.)        */
  140. /* The important thing to remember is that we need to tell the window    */
  141. /* what messages should be allowed to pass by. It was therefore we       */
  142. /* needed to set the IDCMP flags GADGETDOWN and CLOSEWINDOW in the       */
  143. /* NewWindow structure.                                                  */
  144. /*************************************************************************/
  145.  
  146.  
  147.  
  148. main()
  149. {
  150.   /* Boolean variable used for the while loop: */
  151.   BOOL close_me;
  152.  
  153.   /* Declare a variable in which we will store the IDCMP flag: */
  154.   ULONG class;
  155.   
  156.   /* Declare a pointer to an IntuiMessage structure: */
  157.   struct IntuiMessage *my_message;
  158.  
  159.  
  160.  
  161.   /* Before we can use Intuition we need to open the Intuition Library: */
  162.   IntuitionBase = (struct IntuitionBase *)
  163.     OpenLibrary( "intuition.library", 0 );
  164.   
  165.   if( IntuitionBase == NULL )
  166.     exit(); /* Could NOT open the Intuition Library! */
  167.  
  168.  
  169.  
  170.   /* We will now try to open the window: */
  171.   my_window = (struct Window *) OpenWindow( &my_new_window );
  172.   
  173.   /* Have we opened the window succesfully? */
  174.   if(my_window == NULL)
  175.   {
  176.     /* Could NOT open the Window! */
  177.     
  178.     /* Close the Intuition Library since we have opened it: */
  179.     CloseLibrary( IntuitionBase );
  180.  
  181.     exit();  
  182.   }
  183.  
  184.  
  185.  
  186.   /* We have opened the window, and everything seems to be OK. */
  187.  
  188.  
  189.  
  190.   close_me = FALSE;
  191.  
  192.   /* Stay in the while loop until the user has selected the Close window */
  193.   /* gadget: */
  194.   while( close_me == FALSE )
  195.   {
  196.     /* Wait until we have recieved a message: */
  197.     Wait( 1 << my_window->UserPort->mp_SigBit );
  198.  
  199.     /* Collect the message: */
  200.     my_message = (struct IntuiMessage *) GetMsg( my_window->UserPort );
  201.  
  202.     /* Have we collected the message sucessfully? */
  203.     if(my_message)
  204.     {
  205.       /* After we have collected the message we can read it, and save any */
  206.       /* important values which we maybe want to check later: */
  207.       class = my_message->Class;
  208.   
  209.       /* After we have read it we reply as fast as possible: */
  210.       /* REMEMBER! Do never try to read a message after you have replied! */
  211.       /* Some other process has maybe changed it. */
  212.       ReplyMsg( my_message );
  213.   
  214.       /* Check which IDCMP flag was sent: */
  215.       switch( class )
  216.       {
  217.         case CLOSEWINDOW:  /* The user selected the Close window gadget! */
  218.                close_me=TRUE;
  219.                break;
  220.                
  221.         case GADGETDOWN:   /* The user has pressed on the Boolean gadget. */
  222.                printf("Hit\n");
  223.                break;
  224.       }
  225.     }
  226.   }  
  227.  
  228.  
  229.  
  230.   /* We should always close the windows we have opened before we leave: */
  231.   CloseWindow( my_window );
  232.  
  233.  
  234.  
  235.   /* Close the Intuition Library since we have opened it: */
  236.   CloseLibrary( IntuitionBase );
  237.   
  238.   /* THE END */
  239. }
  240.